// 读者写者锁
#include <map>
#include <string>
#include <mutex>
#include <shared_mutex>

class dns_entry { };

class dns_cache {
    std::map<std::string, dns_entry> entries;
    mutable std::shared_mutex entry_mutex;
public:
    // 读共享
    dns_entry find_entry(std::string const& domain) const 
    {
        std::shared_lock<std::shared_mutex> lk(entry_mutex); // 保护共享和只读权限
        std::map<std::string, dns_entry>::const_iterator const it = entries.find(domain);
        return (it == entries.end()) ? dns_entry() : it->second;
    }

    // 写独占
    void upadte_or_add_entry(std::string const& domain, dns_entry const& dns_details)
    {
        // 表格需要更新时，提供独占访问权限
        std::lock_guard<std::shared_mutex> lk(entry_mutex);
        entries[domain] = dns_details;
    }
};